package org.hxyjs.service.impl;

import org.hxyjs.dao.MongodbDao;
import org.hxyjs.dao.IntegralDao;
import org.hxyjs.dto.DTO;
import org.hxyjs.dto.MessageEnum;


import org.hxyjs.entity.Account;
import org.hxyjs.entity.Integral;
import org.hxyjs.page.PageInfo;
import org.hxyjs.service.AccountService;
import org.hxyjs.service.IntegralService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service("integralService")
public class IntegralServiceImpl implements IntegralService, AccountService {
    @Resource
    IntegralDao integralDao;
    @Resource
    MongodbDao mongodbDao;

    @Override
    public DTO<Integral> getAllIntegrals() throws Exception {
        List<Integral> integrals = integralDao.getAllIntegrals();
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum = Optional.ofNullable(integrals).isPresent() ? MessageEnum.GETINTEGRALSUCCESS : MessageEnum.GETINTEGRALSUCCESS;
        return new DTO<Integral>(messageEnum.getCode(), messageEnum.getMessage(), integrals);
    }

    @Override
    public DTO<Integral> getIntegralByUno(String inno) throws Exception {
        Integral integral = integralDao.getIntegralByUno(inno);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum = Optional.ofNullable(inno).isPresent() ? MessageEnum.GETINTEGRALSUCCESS : MessageEnum.GETINTEGRALSUCCESS;
        return new DTO<Integral>(messageEnum.getCode(), messageEnum.getMessage(), integral);
    }

    @Override
    public DTO<Integral> deleteIntegral(String inno) throws Exception {
        Integer result = integralDao.deleteIntegral(inno);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum = result > 0 ? MessageEnum.DELETEINTEGRALSUCCESS : MessageEnum.DELETEINTEGRALFAIL;
        return new DTO<Integral>(messageEnum.getCode(), messageEnum.getMessage());
    }

    @Override
    public DTO<Integral> updateIntegral(Integral integral) throws Exception {
        Integer result = integralDao.updateIntegral(integral);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum = result > 0 ? MessageEnum.UPDATEINTEGRALSUCCESS : MessageEnum.UPDATEINTEGRALFAIL;
        return new DTO<Integral>(messageEnum.getCode(), messageEnum.getMessage());
    }

    @Override
    public DTO<Integral> addIntegralByUno(Integral integral) throws Exception {

        Integer result = integralDao.addIntegralByUno(integral);

        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum = result > 0 ? MessageEnum.ADDINTEGRALSUCCESS : MessageEnum.ADDINTEGRALFAIL;
        return new DTO<Integral>(messageEnum.getCode(), messageEnum.getMessage(), integral);

    }

    @Override
    public DTO<Integral> getIntegralsByPage(PageInfo pageInfo) throws Exception {
        List<Integral> integrals = integralDao.getIntegralsByPage(pageInfo);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum = Optional.ofNullable(integrals).isPresent() ? MessageEnum.GETINTEGRALSUCCESS : MessageEnum.GETINTEGRALFAIL;
        return new DTO<Integral>(messageEnum.getCode(), messageEnum.getMessage(), integrals, pageInfo);
    }

    @Override
    public DTO<Integral> getIntegralsWithOrders(String inno) throws Exception {
        return null;
    }

    /**
     * 查询MongoDB
     *
     * @param inno
     * @return
     * @throws Exception
     */

    @Override
    public DTO<Integral> activate(String inno) throws Exception {
//        Integral integral = new Integral();
//        integral.setUno(inno);
//        integral.setUstate("已激活");
//        Integer result = integralDao.updateIntegral(integral);
//        //isPresent表示判断不为空成立则dto为？与：之间的对象
//        MessageEnum messageEnum = result > 0 ? MessageEnum.UPDATEADMINSUCCESS : MessageEnum.UPDATEADMINFAIL;
//        return new DTO<Integral>(messageEnum.getCode(), messageEnum.getMessage());
        return null;
    }

    @Override
    public DTO<Integral> lettory(String inno,String giftName) throws Exception {
        Integral integral = integralDao.getIntegralByUno(inno);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum = Optional.ofNullable(inno).isPresent() ? MessageEnum.GETINTEGRALSUCCESS : MessageEnum.GETINTEGRALFAIL;
        return new DTO<Integral>(messageEnum.getCode(), messageEnum.getMessage(), integral,giftName);
    }


    @Override
    public DTO<Account> getAccountByAccountName(String accountName) throws Exception {
        DTO<Account> dto = new DTO<Account>();
//        Optional<Account> adminOP = Optional.ofNullable(integralDao.getAccountByAccountName(accountName));
//        dto.setT(adminOP.get());
//        MessageEnum messageEnum = adminOP.isPresent()?MessageEnum.GETADMINSUCCESS:MessageEnum.GETADMINFAIL;
//        dto.setCode(messageEnum.getCode());
//        dto.setMsg(messageEnum.getMessage());
        return dto;
    }
}
